<?php

header('Content-Type: text/html;charset=UTF-8');
$sitemap = '';
$entryresult = '<tr>';
$cid = isset($_REQUEST['cid']) ? intval($_REQUEST['cid']) : 0;
$lv = isset($_REQUEST['lv']) ? intval($_REQUEST['lv']) : 0;
$nt = isset($_REQUEST['nt']) ? intval($_REQUEST['nt']) : 0;

$perPage = 10;
$pageNum = isset($_GET['p']) ? intval($_GET['p']) : 1;
if ($pageNum < 1) {
    $pageNum = 1;
}
if ($nt > 0) {
    $rows = '';
    if (Lang::getCode() == 'en') {
            $nationname = Db::getOneField('name2', 'nation', 'id=' . $nt);
        } else {
            $nationname = Db::getOneField('name', 'nation', 'id=' . $nt);
        }
    if ($lv > 0) {
        $class = 'clred';
        $_SESSION['class'] = 'clred';
        $levelname = '';
        if ($lv == '1') {
            $levelname = "'" . fl('Secondary') . "'";
        } else if ($lv == '2') {
            $levelname = "'" . fl('College') . "'";
        } else if ($lv == '3') {
            $levelname = "'" . fl('University') . "'";
        } else if ($lv == '4') {
            $levelname = "'" . fl('Postgraduate') . "'";
        }
        $sitemap = '<a>' . fl('study') . '</a> > <a>' . $nationname . '</a> > <a href="#">' . $levelname . '</a>';
        $totalRows = Db::getOneField('COUNT(*)', 'article', 'nation=' . $nt . ' and level=' . $lv);
        $paging = Tpl::page($totalRows, $perPage, 'entry?nt=' . $nt . '&lv=' . $lv . '&p=');

        $rows = Db::getRows('*', 'article where nation=' . $nt . ' and level=' . $lv . ' order by id desc limit ' . ($pageNum - 1) * $perPage . ',' . $perPage);
    } elseif ($cid > 0) {
        $class = 'clpink';
        $_SESSION['class'] = 'clpink';
        if (Lang::getCode() == 'en') {
            $catname = Db::getOneField('name2', 'type', 'id=' . $cid);
        } else {
            $catname = Db::getOneField('name', 'type', 'id=' . $cid);
        }
        $sitemap = '<a>' . fl('Student_handbook') . '</a> > <a>' . $catname . '</a> ><a href="#">' . $nationname . '</a>';
        $totalRows = Db::getOneField('COUNT(*)', 'article', 'nation=' . $nt . ' and type=' . $cid);
        $paging = Tpl::page($totalRows, $perPage, 'entry?nt=' . $nt . '&cid=' . $cid . '&p=');

        $rows = Db::getRows('*', 'article where nation=' . $nt . ' and type=' . $cid . ' order by id desc limit ' . ($pageNum - 1) * $perPage . ',' . $perPage);
    }
    foreach ($rows as $value => $row) {
        $endrow = '';
        if (($value % 2 != 0)) {
            $endrow = '</tr><tr>';
        } else {
            $endrow = '';
        }
        $id = $row['id'];
        $title = $row['title'];
        $intro = $row['intro'];
        $title2 = $row['title2'];
        $intro2 = $row['intro2'];
        $avatar = $row['img'];
        $posteddate = $row['posteddate'];
        if (Lang::getCode() == 'en') {
            $entryresult.= '<td valign="top">
            <a class="entry_title ' . $class . '" href="entrycontent?id=' . $id . '">' . $title2 . '</a>
            <div class="datepost">' . $posteddate . '</div>
            <div class="intro">
                <img src="upload/' . $avatar . '" width="190"/>
                ' . $intro2 . '
            </div>
        </td>' . $endrow;
        } else {
            $entryresult.= '<td valign="top">
            <a class="entry_title ' . $class . '" href="entrycontent?id=' . $id . '">' . $title . '</a>
            <div class="datepost">' . $posteddate . '</div>
            <div class="intro">
                <img src="upload/' . $avatar . '" width="190"/>
                ' . $intro . '
            </div>
        </td>' . $endrow;
        }
    }
    $entryresult.= "</tr>";
} else {
    if ($cid > 0) {
        $class = 'clblue';
        $_SESSION['class'] = 'clblue';
        if ($cid == 7) {
            $class = 'clgreen';
            $_SESSION['class'] = 'clgreen';
        }
        if ($cid == 8) {
            $class = 'clyel';
            $_SESSION['class'] = 'clyel';
        }
        $catname = Db::getOneField('name', 'type', 'id=' . $cid);
        $sitemap = '<a>' . $catname . '</a> >';
        $totalRows = Db::getOneField('COUNT(*)', 'article', 'type=' . $cid);
        $paging = Tpl::page($totalRows, $perPage, 'entry?cid=' . $cid . '&p=');

        $rows = Db::getRows('*', 'article where type=' . $cid . ' order by id desc limit ' . ($pageNum - 1) * $perPage . ',' . $perPage);
        if ($lv > 0) {
            $totalRows = Db::getOneField('COUNT(*)', 'article', 'type=' . $cid . ' and level=' . $lv);
            $paging = Tpl::page($totalRows, $perPage, 'entry?cid=' . $cid . '&lv=' . $lv . '&p=');

            $rows = Db::getRows('*', 'article where type=' . $cid . ' and level=' . $lv . ' order by id desc limit ' . ($pageNum - 1) * $perPage . ',' . $perPage);
        }
    }

    foreach ($rows as $value => $row) {
        $endrow = '';
        if (($value % 2 != 0)) {
            $endrow = '</tr><tr>';
        } else {
            $endrow = '';
        }
        $id = $row['id'];
        $title = $row['title'];
        $intro = $row['intro'];
        $title2 = $row['title2'];
        $intro2 = $row['intro2'];
        $avatar = $row['img'];
        $posteddate = $row['posteddate'];
        if (Lang::getCode() == 'en') {
            $entryresult.= '<td valign="top">
            <a class="entry_title ' . $class . '" href="entrycontent?id=' . $id . '">' . $title2 . '</a>
            <div class="datepost">' . $posteddate . '</div>
            <div class="intro">
                <img src="upload/' . $avatar . '" width="190"/>
                ' . $intro2 . '
            </div>
        </td>' . $endrow;
        } else {
            $entryresult.= '<td valign="top">
            <a class="entry_title ' . $class . '" href="entrycontent?id=' . $id . '">' . $title . '</a>
            <div class="datepost">' . $posteddate . '</div>
            <div class="intro">
                <img src="upload/' . $avatar . '" width="190"/>
                ' . $intro . '
            </div>
        </td>' . $endrow;
        }
    }
    $entryresult.= "</tr>";
}
?>
